package com.ds.infra.rpc.registry.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
 * 作用：RPC服务实例模型类，包含服务基本信息
 *
 * @author WeiShaoying
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ServiceInstance {

    private String serviceName;
    private String host;
    private int port;
    private Map<String, Object> metadata;

    public ServiceInstance(String serviceName, String host, int port) {
        this.serviceName = serviceName;
        this.host = host;
        this.port = port;
    }

    // 从Map转换的工厂方法
    public static ServiceInstance fromMap(Map<String, Object> map) {
        String address = (String) map.get("serviceAddress");
        String[] hostPort = address.split(":");
        return new ServiceInstance(
                (String) map.get("serviceName"),
                hostPort[0],
                Integer.parseInt(hostPort[1])
        );
    }
}
